package com.yunhe.ssm.struts.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.yunhe.ssm.mybatis.model.Page;
import com.yunhe.ssm.mybatis.model.Teacher;
import com.yunhe.ssm.mybatis.service.ITeacherService;

public class TeacherAction extends ActionSupport implements ModelDriven<Object>{
	@Autowired
	private ITeacherService teacherservice;
	private Teacher teacher;
	private Page page;
	private int pageSize;
	private int curPage;
	
	
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getCurPage() {
		return curPage;
	}
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	@Override
	public Object getModel() {
		if(teacher==null){
			teacher=new Teacher();
		}
		return teacher;
	}
	
	HttpServletRequest request = ServletActionContext.getRequest();
	HttpServletResponse response = ServletActionContext.getResponse();
public void teacherLogin() throws IOException{
	PrintWriter out=response.getWriter();
	Teacher tea=new Teacher();
	
	tea=teacherservice.teacherLogin(teacher);

	if(tea!=null){
		out.print(JSON.toJSON(tea));
	}
}
public String getTeacherById() throws IOException{
	 response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
	PrintWriter out=response.getWriter();
	System.out.println(teacherservice.getTeacherById(teacher).getTeaname());
	request.setAttribute("teacher1", teacherservice.getTeacherById(teacher));
	return SUCCESS;
	
}

public String getTeacherPage(){
	Map<String,Object> cond=new HashMap<String,Object>();
	cond.put("pageSize", pageSize);
	cond.put("pageNum", curPage);
	page=teacherservice.getTeacherPage(cond);
	return SUCCESS;
	
}
}
